package com.nzd.slsharing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nzd.slsharing.dto.UserCommentDto;
import com.nzd.slsharing.pojo.*;
import com.nzd.slsharing.service.CommentService;
import com.nzd.slsharing.service.PostSectionService;
import com.nzd.slsharing.service.UserService;
import com.nzd.slsharing.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

//评论控制器
@RestController
@RequestMapping("/api/Comment")
public class CommentController {
    @Resource
    private CommentService commentService;

    @Resource
    private UserService userService;

    @Resource
    private PostSectionService postSectionService;


    //分页获取所有评论(不带分页)
    @GetMapping("/list")
    public Result findAll() {
        return Result.ok(commentService.list());
    }

    @GetMapping("/pageList")
    public Result findPage(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(required = false, defaultValue = "3") Integer pageSize,
                           @RequestParam(required = false, defaultValue = "") Integer postId,
                           @RequestParam(required = false, defaultValue = "") Integer commentType) {

//        System.out.println("帖子id"+postId);
//        System.out.println("帖子类型"+commentType);
//        System.out.println("页"+pageNum);
//        System.out.println("数"+pageSize);

        // 设置分页参数
        Page<Comment> page = new Page<>(pageNum, pageSize);

        // 查询评论列表
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        if (postId != null && postId > 0) {  // 如果 postId 有值并且大于 0
            queryWrapper.eq("post_id", postId);
        }
        if (commentType != null && commentType > 0) {  // 如果 commentType 有值并且大于 0
            queryWrapper.eq("comment_type", commentType);
        }

        // 获取分页查询结果
        IPage<Comment> commentPage = commentService.page(page, queryWrapper);

        // 填充用户信息到每个评论中
        for (Comment comment : commentPage.getRecords()) {
            // 查找用户信息并填充
            User user = userService.getOne(new QueryWrapper<User>().eq("student_id", comment.getStudentId()).ne("user_role", 3));
            //查找评论分类名称并填充
            PostSection postSection = postSectionService.getOne(new QueryWrapper<PostSection>().eq("type_code", comment.getCommentType()));
            UserCommentDto userCommentDto = new UserCommentDto();
            if (user != null) {
                BeanUtils.copyProperties(user, userCommentDto);
            }
            comment.setCommentTypeName(postSection.getTypeName());
            comment.setUserCommentDto(userCommentDto);  // 填充用户信息到评论中
        }

        // 构建返回的分页数据
        Map<String, Object> data = new HashMap<>();
        data.put("comments", commentPage.getRecords());  // 评论列表
        data.put("total", commentPage.getTotal());  // 总记录数
        data.put("pages", commentPage.getPages());  // 总页数
        data.put("current", commentPage.getCurrent());  // 当前页
        data.put("size", commentPage.getSize());  // 每页条数

        // 返回分页数据
        return Result.ok(data);
    }

    //修改
    @PutMapping("/update")
    public Result update(@RequestBody Comment comment) {
        return Result.ok(commentService.updateById(comment));
    }


    //删除
    @DeleteMapping("/{id}")
    public Result del(@PathVariable Long id) {
        return Result.ok(commentService.removeById(id));
    }

}

